package com.example.demo.service;

import org.flowable.engine.delegate.DelegateExecution;
import org.flowable.engine.delegate.JavaDelegate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.HashMap;
import java.util.Map;

/**
 * 请假审批服务
 * 处理员工请假审批流程中的业务逻辑
 */
@Service
public class LeaveApprovalService implements JavaDelegate {

    @Autowired
    private NotificationService notificationService;

    /**
     * 处理直接审批（短期请假）
     */
    public void handleDirectApproval(DelegateExecution execution) {
        System.out.println("处理直接审批 - 短期请假");
        
        // 获取流程变量
        String applicant = (String) execution.getVariable("applicant");
        Integer leaveDays = (Integer) execution.getVariable("leaveDays");
        String reason = (String) execution.getVariable("reason");
        
        // 业务逻辑处理
        boolean approved = processDirectApproval(applicant, leaveDays, reason);
        
        // 设置审批结果
        execution.setVariable("approved", approved);
        execution.setVariable("approveTime", LocalDateTime.now());
        execution.setVariable("approver", "direct_manager");
        
        System.out.println("直接审批完成 - 申请人: " + applicant + ", 结果: " + (approved ? "通过" : "拒绝"));
    }

    /**
     * 处理HR审批
     */
    public void handleHrApproval(DelegateExecution execution) {
        System.out.println("处理HR审批");
        
        String applicant = (String) execution.getVariable("applicant");
        String leaveType = (String) execution.getVariable("leaveType");
        Integer leaveDays = (Integer) execution.getVariable("leaveDays");
        
        // 业务逻辑处理
        boolean approved = processHrApproval(applicant, leaveType, leaveDays);
        
        // 设置审批结果
        execution.setVariable("hrApproved", approved);
        execution.setVariable("hrApproveTime", LocalDateTime.now());
        execution.setVariable("hrApprover", "hr_manager");
        
        System.out.println("HR审批完成 - 申请人: " + applicant + ", 结果: " + (approved ? "通过" : "拒绝"));
    }

    /**
     * 处理经理审批
     */
    public void handleManagerApproval(DelegateExecution execution) {
        System.out.println("处理经理审批");
        
        String applicant = (String) execution.getVariable("applicant");
        Integer leaveDays = (Integer) execution.getVariable("leaveDays");
        String reason = (String) execution.getVariable("reason");
        
        // 业务逻辑处理
        boolean approved = processManagerApproval(applicant, leaveDays, reason);
        
        // 设置审批结果
        execution.setVariable("managerApproved", approved);
        execution.setVariable("managerApproveTime", LocalDateTime.now());
        execution.setVariable("managerApprover", "department_manager");
        
        System.out.println("经理审批完成 - 申请人: " + applicant + ", 结果: " + (approved ? "通过" : "拒绝"));
    }

    /**
     * 处理直接审批业务逻辑
     */
    private boolean processDirectApproval(String applicant, Integer leaveDays, String reason) {
        // 简单的业务规则
        if (leaveDays <= 1) {
            return true; // 1天以内直接通过
        } else if (leaveDays <= 3) {
            // 2-3天需要简单审核
            return reason != null && reason.length() > 10;
        }
        return false;
    }

    /**
     * 处理HR审批业务逻辑
     */
    private boolean processHrApproval(String applicant, String leaveType, Integer leaveDays) {
        // HR审批规则
        if ("sick".equals(leaveType)) {
            return true; // 病假通常直接通过
        } else if ("personal".equals(leaveType)) {
            return leaveDays <= 5; // 事假不超过5天
        } else if ("annual".equals(leaveType)) {
            return leaveDays <= 10; // 年假不超过10天
        }
        return false;
    }

    /**
     * 处理经理审批业务逻辑
     */
    private boolean processManagerApproval(String applicant, Integer leaveDays, String reason) {
        // 经理审批规则
        if (leaveDays <= 7) {
            return true; // 一周以内通常通过
        } else if (leaveDays <= 14) {
            return reason != null && reason.length() > 20; // 需要详细说明
        }
        return false;
    }

    /**
     * 发送审批结果通知
     */
    public void sendApprovalNotification(DelegateExecution execution) {
        String applicant = (String) execution.getVariable("applicant");
        boolean approved = (Boolean) execution.getVariable("approved");
        
        Map<String, Object> notificationData = new HashMap<>();
        notificationData.put("applicant", applicant);
        notificationData.put("approved", approved);
        notificationData.put("approveTime", LocalDateTime.now());
        
        // 发送通知
        // notificationService.sendLeaveApprovalNotification(notificationData);
        System.out.println("发送通知数据: " + notificationData);
        
        System.out.println("审批结果通知已发送 - 申请人: " + applicant);
    }

    @Override
    public void execute(DelegateExecution execution) {
        // 实现 JavaDelegate 接口
        System.out.println("执行请假审批服务");
    }
}

